<template>
  <el-dialog
    :close-on-click-modal="false"
    :append-to-body="true"
    title="切换岗位"
    width="800px"
    @close="handleCancel"
    :visible.sync="visible"
  >

    <el-radio-group v-if="positionList && positionList.length" v-model="positionId" size="small">
      <el-radio
        v-for="(item,index) in positionList"
        :key="index"
        :label="item.positionId"
        border
      >{{ item.deptName + '-' + item.positionName }}</el-radio>
      <!-- <el-radio label="2" border>备选项2</el-radio> -->
    </el-radio-group>
    <empty-comp v-else msg="暂无岗位" />
    
    <div slot="footer">
      <el-button @click="handleCancel">取消</el-button>
      <el-button @click="handleSure" type="primary">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import EmptyComp from '@/components/EmptyComp'
import { getDeptAndPositionList } from '@/api/system'
export default {
  data() {
    return {
      isEdit: false,
      visible: false,
      userInfo: {},
      positionId: '',
      positionList: [],
    }
  },
  mounted() {

  },
  methods: {
    show() {
      this.visible = true
      const userInfo = this.$store.getters.userInfo
      this.userInfo = userInfo || {}
      this.positionId = this.userInfo.positionId
      this.getDeptAndPositionList()
    },
    getDeptAndPositionList() {
      if (!this.userInfo.userId) return
      getDeptAndPositionList({ 
        userId: this.userInfo.userId
      }).then( async res => {
        this.positionList = res.data
      })
    },
    handleCancel() {
      this.visible = false
    },
    handleSure() {
      if (this.positionId && this.positionId !== this.userInfo.positionId) {
        const position = this.positionList.find(v => v.positionId === this.positionId)
        this.$store.dispatch('changeLoginPosition', position).then(() => { // 切换角色
          this.$message.success('切换成功')
          // this.$router.replace({ path: '/desktop/index' })
          setTimeout(function() {
            window.location.href = '/'
          }, 500)
        })
      } else {
        this.$message.warning('您选择的是当前岗位，不需要切换')
        return
      }
      this.visible = false
    }
  }
}
</script>
